<?php
require_once '../config/config.php';


if (isset($_GET["logout"])) {
    session_unset();
    session_destroy();
    header('Location: index.php');
    exit();
}

if (isset($_SESSION["admin_userid"])) {
    header("location:tableau_bord.php");
    exit();
}


if (isset($_POST['connexion'])) {
    $sql = 'SELECT * FROM `admin` WHERE login = "' . $_POST['login'] . '" AND password = "' . $_POST['password'] . '"';
    $req = mysql_query($sql) or die('Unable to run query:' . mysql_error());
    $num = mysql_num_rows($req);
    if ($num == 1) {
        $res = mysql_fetch_array($req);
        $_SESSION["admin_userid"] = $res["id"];
        $_SESSION["admin_username"] = $res["nom"] . " " . $res["prenom"];
        $_SESSION["admin_usermail"] = $res["email"];
        $_SESSION["admin_usertype"] = $res["type"];
        switch ($res["type"]) {
            case 1:
                header('Location: tableau_bord.php');
                exit();
                break;
            case 2:
                header('Location: gestion_offre.php');
                exit();
                break;
            case 3:
                header('Location: edition_lettre.php');
                exit();
                break;
            default:
                break;
        }
    }
    else
        $messages_erreur = "Mauvais  nom d'utilisateur / mot de passe, merci d'essayer à nouveau.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
        <title>Interface Administration | L'essentiel du sup</title>
        <link rel="shortcut icon" href="images/favicon.ico" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <link href="css/login.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/jquery-1.7.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript" src="js/jquery.validationEngine-fr.js"></script>
        <script type="text/javascript" src="js/jquery.validationEngine.js"></script>
        <script type="text/javascript" src="js/custom/general.js"></script>
        <script type="text/javascript">
            jQuery(function() {
                jQuery("#validate").validationEngine();
            });
        </script>
    </head>
    <body class="loginPage" style="background: none repeat scroll 0 0 #F1F1F2;">
        <?php if (!empty($messages_erreur)) {
            ?>
            <div class="notification msgerror">
                <a class="close"></a>
                <p><?php echo $messages_erreur; ?></p>
            </div>

        <?php } ?>
        <div class="loginWrapper">
            <div class="loginLogo" style="text-align: center; top: -100px; left: 47%;">
                <img alt="" src="images/logo.png" />
            </div>
            <div class="widget">
                <div class="title"><img src="images/icons/dark/key.png" alt="" class="titleIcon" /><h6>Connexion</h6></div>
                <form action="" method="post" id="validate" class="form">
                    <fieldset>
                        <div class="formRow">
                            <label for="login">Nom d'utilisateur:</label>
                            <div class="loginInput"><input type="text" name="login" class="validate[required]" id="login" /></div>
                            <div class="clear"></div>
                        </div>
                        <div class="formRow">
                            <label for="pass">Mot de passe:</label>
                            <div class="loginInput"><input type="password" name="password" class="validate[required]" id="pass" /></div>
                            <div class="clear"></div>
                        </div>
                        <div class="loginControl" style="float: right">
                            <input type="submit" name="connexion" value="Connexion"
                                   class="button blackB" />
                            <div class="clear"></div>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
        <div id="footer">
            <div class="wrapper"></div>
        </div>
    </body>
</html>